var express = require('express')
    , router = express.Router()
    , roomManager = require('../service/roomManager')()
    , C = require('../helper/core')

function is_input_valid(req) {
    var peopleNum = req.body.peopleNum
    return peopleNum != null && peopleNum < 20 && peopleNum > 1;
}

router.post('/createRoom', (req, res) => {

    if (!C.is_logined(req)) {
        res.json(C.expired_result())
        return
    }

    var result = {
        msg: '',
        ok: true,
        data: null,
    }

    if (!is_input_valid(req)) {
        result.ok = false
        result.msg = '请提供正确的参数'
        res.json(result)
        return
    }

    var people = {
        id: req.session.id,
        name: req.session.user.nickName,
        avatar: req.session.user.avatarUrl,
    }
    var peopleNum = req.body.peopleNum
    var room = roomManager.createRoom(people, peopleNum)

    if (!room) {
        result.msg = '创建房间失败'
        result.ok = false
        res.json(result)
        return
    }

    result.data = room.getInfo()
    req.session.room = room

    res.json(result)
})

//通过用户的session信息来判断所在房间并且返回房间信息
router.get('/getRoomInfo', (req, res) => {
    var result = {
        msg: '',
        result: 'success',
        data: null,
    }

    var room = req.session.room

    if (!room) {
        result.msg = '您不在房间内'
        result.result = 'fail'
        return result
    }
    //room.peoples.push('test')

    result.data = room.getInfo()

    res.json(result)
})

router.post('/enterRoom', (req, res) => {
    var result = {
        msg: '',
        result: 'success',
        data: null,
    }

    var roomNum = req.body.roomNum

    if (!roomNum) {
        result.msg = '没有这个房间'
        result.result = 'fail'
        res.json(result)
        return
    }

    var people = {
        id: req.session.id,
        name: req.session.user.nickName,
        avatar: req.session.user.avatarUrl,
    }
    var room = roomManager.enterRoom(people, roomNum)
    if (!room) {
        result.msg = '没有这个房间或者人满了'
        result.result = 'fail'
        res.json(result)
        return
    }
    result.data = room.getInfo()
    req.session.room = room

    res.json(result)
})

router.get('/exitRoom', (req, res) => {
    var result = {
        msg: '',
        result: 'success',
        data: null,
    }

    var room = req.session.room

    if (!room) {
        result.msg = '您不在房间内'
        result.result = 'fail'
        return result
    }

    roomManager.exitRoom(room.roomNum, req.session.id)

    res.json(result)
})

router.get('/ready', (req, res) => {
    var result = {
        msg: '',
        result: 'success',
        data: null,
    }

    var room = req.session.room

    if (!room) {
        result.msg = '您不在房间内'
        result.result = 'fail'
        return result
    }

    room.ready(req.session.id)

    res.json(result)
})
router.get('/ready', (req, res) => {
    var result = {
        msg: '',
        result: 'success',
        data: null,
    }

    var room = req.session.room

    if (!room) {
        result.msg = '您不在房间内'
        result.result = 'fail'
        return result
    }

    room.ready(req.session.id)

    res.json(result)
})

router.get('/unReady', (req, res) => {
    var result = {
        msg: '',
        result: 'success',
        data: null,
    }

    var room = req.session.room

    if (!room) {
        result.msg = '您不在房间内'
        result.result = 'fail'
        return result
    }

    room.unReady(req.session.id)

    res.json(result)
})

module.exports = router